package com.fecean.aabbc.commons.easy;

import java.math.BigDecimal;
import java.math.BigInteger;
import java.math.RoundingMode;
import java.util.Objects;
import java.util.Optional;

/**
 ** 通用对象类，可进行常用类型的转换
 *
 * @author: <a href="mailto:chenfenghai@oristartech.com">chenfenghai</a>
 * @version: 1.0
 * @date: 2019-10-21 14:18
 */
public class EasyConverter {

    /**
     * If non-null, the value; if null, indicates no value is present
     */
    private Object object;

    /**
     ** The object of BigDecimal
     */
    private BigDecimal bigDecimal;

    //-----------------------------------------------------------------------------------------------------------------
    /**
     * Constructs an empty instance.
     */
    private EasyConverter() {
        throw new AssertionError("No Converter instances for you!");
    }

    /**
     * Constructs an instance with the value present.
     */
    public EasyConverter(Object object) {
        this.object = object;
        this.bigDecimal = null;
    }

    //-----------------------------------------------------------------------------------------------------------------
    /**
     ** 创建实例
     *
     * @param object 输入参数
     * @return {@code Converter}, not null
     */
    public static EasyConverter of(final Object object){
        return new EasyConverter(object);
    }

    //-----------------------------------------------------------------------------------------------------------------
    /**
     ** 创建实例
     *
     * @param object 输入参数
     * @return {@code Converter}, not null
     */
    public static EasyConverter of(final Object object, final Object defaultValue){
        try{
            return ofThrow(object);
        }catch (Exception ex){
            return of(defaultValue);
        }
    }

    //-----------------------------------------------------------------------------------------------------------------
    /**
     ** 创建实例，如果参数为null，则抛出异常
     *
     * @param object 输入参数
     * @return {@code Converter}, not null
     * @throws NullPointerException if {@code object} is {@code null}
     */
    public static EasyConverter ofThrow(final Object object){
        Objects.requireNonNull(object, "The object value cannot be null");
        return new EasyConverter(object);
    }

    //-----------------------------------------------------------------------------------------------------------------
    /**
     ** 转换为int，如果转换失败则默认为0
     *
     * @return a int value, not null
     * @see #intValueDefault(Object)
     */
    public int intValue(){
        return intValueDefault(0);
    }

    //-----------------------------------------------------------------------------------------------------------------
    /**
     ** 转换为int，如果转换失败则默认为0
     *
     * @param roundingMode 舍入模式，为空时默认是四舍五入模式RoundingMode.HALF_UP
     * @return a int value, not null
     * @see #intValueDefault(RoundingMode, Object)
     */
    public int intValue(final RoundingMode roundingMode){
        return intValueDefault(roundingMode, 0);
    }

    //-----------------------------------------------------------------------------------------------------------------
    /**
     ** 转换为int
     *
     * @param defaultNumber 当转换出错时默认数字值
     * @return a int value, not null
     * @see #intValueThrow()
     * @see #of(Object)
     * @see #intValue()
     */
    public int intValueDefault(final Object defaultNumber){
        try {
            return intValueThrow();
        }catch (NullPointerException | NumberFormatException ex){
            return of(defaultNumber).intValue();
        }
    }

    //-----------------------------------------------------------------------------------------------------------------
    /**
     ** 转换为int
     *
     * @param roundingMode 舍入模式，为空时默认是四舍五入模式RoundingMode.HALF_UP
     * @param defaultNumber 当转换出错时默认数字值
     * @return a int value, not null
     * @see #intValueThrow(RoundingMode)
     * @see #of(Object)
     * @see #intValue(RoundingMode)
     */
    public int intValueDefault(final RoundingMode roundingMode, final Object defaultNumber){
        try {
            return intValueThrow(roundingMode);
        }catch (NullPointerException | NumberFormatException ex){
            return of(defaultNumber).intValue(roundingMode);
        }
    }

    //-----------------------------------------------------------------------------------------------------------------
    /**
     ** 转换为int，如果转换失败则抛出异常
     *
     * @return a int value, not null
     * @throws NullPointerException if {@code number} is {@code null}
     * @throws NumberFormatException if {@code number} is not a valid representation of a {@code BigDecimal}.
     * @see #toBigDecimalThrow()
     * @see BigDecimal#intValue()
     */
    public int intValueThrow(){
        return toBigDecimalThrow().intValue();
    }

    //-----------------------------------------------------------------------------------------------------------------
    /**
     ** 转换为int，如果转换失败则抛出异常
     *
     * @param roundingMode 舍入模式，为空时默认是四舍五入模式RoundingMode.HALF_UP
     * @return a int value, not null
     * @throws NullPointerException if {@code number} is {@code null}
     * @throws NumberFormatException if {@code number} is not a valid representation of a {@code BigDecimal}.
     * @see #toBigDecimalThrow(int, RoundingMode)
     * @see BigDecimal#intValue()
     */
    public int intValueThrow(final RoundingMode roundingMode){
        return toBigDecimalThrow(0, roundingMode).intValue();
    }

    //-----------------------------------------------------------------------------------------------------------------
    /**
     ** 转换为long，如果转换失败则默认为0
     *
     * @return a long value, not null
     * @see #longValueDefault(Object)
     */
    public long longValue(){
        return longValueDefault(0);
    }

    //-----------------------------------------------------------------------------------------------------------------
    /**
     ** 转换为long，如果转换失败则默认为0
     *
     * @param roundingMode 舍入模式，为空时默认是四舍五入模式RoundingMode.HALF_UP
     * @return a long value, not null
     * @see #longValueDefault(RoundingMode, Object)
     */
    public long longValue(final RoundingMode roundingMode){
        return longValueDefault(roundingMode, 0);
    }

    //-----------------------------------------------------------------------------------------------------------------
    /**
     ** 转换为long
     *
     * @param defaultNumber 当转换出错时默认数字值
     * @return a long value, not null
     * @see #longValueThrow()
     * @see #of(Object)
     * @see #longValue()
     */
    public long longValueDefault(final Object defaultNumber){
        try {
            return longValueThrow();
        }catch (NullPointerException | NumberFormatException ex){
            return of(defaultNumber).longValue();
        }
    }

    //-----------------------------------------------------------------------------------------------------------------
    /**
     ** 转换为long
     *
     * @param roundingMode 舍入模式，为空时默认是四舍五入模式RoundingMode.HALF_UP
     * @param defaultNumber 当转换出错时默认数字值
     * @return a long value, not null
     * @see #longValueThrow(RoundingMode)
     * @see #of(Object)
     * @see #longValue(RoundingMode)
     */
    public long longValueDefault(final RoundingMode roundingMode, final Object defaultNumber){
        try {
            return longValueThrow(roundingMode);
        }catch (NullPointerException | NumberFormatException ex){
            return of(defaultNumber).longValue(roundingMode);
        }
    }

    //-----------------------------------------------------------------------------------------------------------------
    /**
     ** 转换为long，如果转换失败则抛出异常
     *
     * @return a long value, not null
     * @throws NullPointerException if {@code number} is {@code null}
     * @throws NumberFormatException if {@code number} is not a valid representation of a {@code BigDecimal}.
     * @see #toBigDecimalThrow()
     * @see BigDecimal#longValue()
     */
    public long longValueThrow(){
        return toBigDecimalThrow().longValue();
    }

    //-----------------------------------------------------------------------------------------------------------------
    /**
     ** 转换为long，如果转换失败则抛出异常
     *
     * @param roundingMode 舍入模式，为空时默认是四舍五入模式RoundingMode.HALF_UP
     * @return a long value, not null
     * @throws NullPointerException if {@code number} is {@code null}
     * @throws NumberFormatException if {@code number} is not a valid representation of a {@code BigDecimal}.
     * @see #toBigDecimalThrow(int, RoundingMode)
     * @see BigDecimal#longValue()
     */
    public long longValueThrow(final RoundingMode roundingMode){
        return toBigDecimalThrow(0, roundingMode).longValue();
    }

    //-----------------------------------------------------------------------------------------------------------------
    /**
     ** 转换为float，如果转换失败则默认为0
     *
     * @return a float value, not null
     * @see #floatValueDefault(Object)
     */
    public float floatValue(){
        return floatValueDefault(0);
    }

    //-----------------------------------------------------------------------------------------------------------------
    /**
     ** 转换为float，舍入模式默认是四舍五入RoundingMode.HALF_UP，如果转换失败则默认为0
     *
     * @param scale 保存小数点后面的精度，即小数位数
     * @return a float value, not null
     * @see #floatValueDefault(int, RoundingMode, Object)
     */
    public float floatValue(final int scale){
        return floatValueDefault(scale, RoundingMode.HALF_UP, 0);
    }

    //-----------------------------------------------------------------------------------------------------------------
    /**
     ** 转换为float，如果转换失败则默认为0
     *
     * @param scale 保存小数点后面的精度，即小数位数
     * @param roundingMode 舍入模式，为空时默认是四舍五入模式RoundingMode.HALF_UP
     * @return a float value, not null
     * @see #floatValueDefault(int, RoundingMode, Object)
     */
    public float floatValue(final int scale, final RoundingMode roundingMode){
        return floatValueDefault(scale, roundingMode, 0);
    }

    //-----------------------------------------------------------------------------------------------------------------
    /**
     ** 转换为float，如果转换失败则默认为0
     *
     * @param defaultNumber 当转换出错时默认数字值
     * @return a float value, not null
     * @see #floatValueThrow()
     * @see #of(Object)
     * @see #floatValue()
     */
    public float floatValueDefault(final Object defaultNumber){
        try {
            return floatValueThrow();
        }catch (Exception ex){
            return of(defaultNumber).floatValue();
        }
    }

    //-----------------------------------------------------------------------------------------------------------------
    /**
     ** 转换为float，舍入模式默认是四舍五入RoundingMode.HALF_UP，如果转换失败则默认为0
     *
     * @param scale 保存小数点后面的精度，即小数位数
     * @param defaultNumber 当转换出错时默认数字值
     * @return a float value, not null
     * @see #floatValueDefault(int, RoundingMode, Object)
     */
    public float floatValueDefault(final int scale, final Object defaultNumber){
        return floatValueDefault(scale, RoundingMode.HALF_UP, defaultNumber);
    }

    //-----------------------------------------------------------------------------------------------------------------
    /**
     ** 转换为float，如果转换失败则默认为0
     *
     * @param scale 保存小数点后面的精度，即小数位数
     * @param roundingMode 舍入模式，为空时默认是四舍五入模式RoundingMode.HALF_UP
     * @param defaultNumber 当转换出错时默认数字值
     * @return a float value, not null
     * @see #floatValueThrow(int, RoundingMode)
     * @see #of(Object)
     * @see #floatValue(int, RoundingMode)
     */
    public float floatValueDefault(final int scale, final RoundingMode roundingMode, final Object defaultNumber){
        try {
            return floatValueThrow(scale, roundingMode);
        }catch (NullPointerException | NumberFormatException ex){
            return of(defaultNumber).floatValue(scale, roundingMode);
        }
    }

    //-----------------------------------------------------------------------------------------------------------------
    /**
     ** 转换为float，如果转换失败则抛出异常
     *
     * @return a float value, not null
     * @throws NullPointerException if {@code number} is {@code null}
     * @throws NumberFormatException if {@code number} is not a valid representation of a {@code BigDecimal}.
     * @see #toBigDecimalThrow()
     * @see BigDecimal#floatValue()
     */
    public float floatValueThrow(){
        return toBigDecimalThrow().floatValue();
    }

    //-----------------------------------------------------------------------------------------------------------------
    /**
     ** 转换为float，舍入模式默认是四舍五入RoundingMode.HALF_UP，如果转换失败则抛出异常
     *
     * @param scale 保存小数点后面的精度，即小数位数
     * @return a float value, not null
     * @throws NullPointerException if {@code number} is {@code null}
     * @throws NumberFormatException if {@code number} is not a valid representation of a {@code BigDecimal}.
     * @see #floatValueThrow(int, RoundingMode)
     */
    public float floatValueThrow(final int scale){
        return floatValueThrow(scale, RoundingMode.HALF_UP);
    }

    //-----------------------------------------------------------------------------------------------------------------
    /**
     ** 转换为Float，如果转换失败则抛出异常
     *
     * @param scale 保存小数点后面的精度，即小数位数
     * @param roundingMode 舍入模式，为空时默认是四舍五入模式RoundingMode.HALF_UP
     * @return a float value, not null
     * @throws NullPointerException if {@code number} is {@code null}
     * @throws NumberFormatException if {@code number} is not a valid representation of a {@code BigDecimal}.
     * @see #toBigDecimalThrow(int, RoundingMode)
     * @see BigDecimal#floatValue()
     */
    public float floatValueThrow(final int scale, final RoundingMode roundingMode){
        return toBigDecimalThrow(scale, roundingMode).floatValue();
    }

    //-----------------------------------------------------------------------------------------------------------------
    /**
     ** 转换为double，如果转换失败则默认为0
     *
     * @return a double value, not null
     * @see #doubleValueDefault(Object)
     */
    public double doubleValue(){
        return doubleValueDefault(0);
    }

    //-----------------------------------------------------------------------------------------------------------------
    /**
     ** 转换为double，舍入模式默认是四舍五入RoundingMode.HALF_UP，如果转换失败则默认为0
     *
     * @param scale 保存小数点后面的精度，即小数位数
     * @return a double value, not null
     * @see #doubleValueDefault(int, Object)
     */
    public double doubleValue(final int scale){
        return doubleValueDefault(scale, 0);
    }

    //-----------------------------------------------------------------------------------------------------------------
    /**
     ** 转换为double，如果转换失败则默认为0
     *
     * @param scale 保存小数点后面的精度，即小数位数
     * @param roundingMode 舍入模式，为空时默认是四舍五入模式RoundingMode.HALF_UP
     * @return a double value, not null
     * @see #doubleValueDefault(int, RoundingMode, Object)
     */
    public double doubleValue(final int scale, final RoundingMode roundingMode){
        return doubleValueDefault(scale, roundingMode, 0);
    }

    //-----------------------------------------------------------------------------------------------------------------
    /**
     ** 转换为double
     *
     * @param defaultNumber 当转换出错时默认数字值
     * @return a double value, not null
     * @see #doubleValueThrow()
     * @see #of(Object)
     * @see #doubleValue()
     */
    public double doubleValueDefault(final Object defaultNumber){
        try {
            return doubleValueThrow();
        }catch (NullPointerException | NumberFormatException ex){
            return of(defaultNumber).doubleValue();
        }
    }

    //-----------------------------------------------------------------------------------------------------------------
    /**
     ** 转换为double，舍入模式默认是四舍五入RoundingMode.HALF_UP
     *
     * @param scale 保存小数点后面的精度，即小数位数
     * @param defaultNumber 当转换出错时默认数字值
     * @return a double value, not null
     * @see #doubleValueDefault(int, RoundingMode, Object)
     */
    public double doubleValueDefault(final int scale, final Object defaultNumber){
        return doubleValueDefault(scale, RoundingMode.HALF_UP, defaultNumber);
    }

    //-----------------------------------------------------------------------------------------------------------------
    /**
     ** 转换为double
     *
     * @param scale 保存小数点后面的精度，即小数位数
     * @param roundingMode 舍入模式，为空时默认是四舍五入模式RoundingMode.HALF_UP
     * @param defaultNumber 当转换出错时默认数字值
     * @return a double value, not null
     * @see #doubleValue(int, RoundingMode)
     * @see #of(Object)
     * @see #doubleValue(int, RoundingMode)
     */
    public double doubleValueDefault(final int scale, final RoundingMode roundingMode, final Object defaultNumber){
        try {
            return doubleValueThrow(scale, roundingMode);
        }catch (NullPointerException | NumberFormatException ex){
            return of(defaultNumber).doubleValue(scale, roundingMode);
        }
    }

    //-----------------------------------------------------------------------------------------------------------------
    /**
     ** 转换为double，如果转换失败则抛出异常
     *
     * @return a double value, not null
     * @throws NullPointerException if {@code number} is {@code null}
     * @throws NumberFormatException if {@code number} is not a valid representation of a {@code BigDecimal}.
     * @see #toBigDecimalThrow()
     * @see BigDecimal#doubleValue()
     */
    public Double doubleValueThrow(){
        return toBigDecimalThrow().doubleValue();
    }

    //-----------------------------------------------------------------------------------------------------------------
    /**
     ** 转换为double，舍入模式默认是四舍五入RoundingMode.HALF_UP，如果转换失败则抛出异常
     *
     * @param scale 保存小数点后面的精度，即小数位数
     * @return a double value, not null
     * @throws NullPointerException if {@code number} is {@code null}
     * @throws NumberFormatException if {@code number} is not a valid representation of a {@code BigDecimal}.
     * @see #doubleValueThrow(int, RoundingMode)
     */
    public Double doubleValueThrow(final int scale){
        return doubleValueThrow(scale, RoundingMode.HALF_UP);
    }

    //-----------------------------------------------------------------------------------------------------------------
    /**
     ** 转换为double，如果转换失败则抛出异常
     *
     * @param scale 保存小数点后面的精度，即小数位数
     * @param roundingMode 舍入模式，为空时默认是四舍五入模式RoundingMode.HALF_UP
     * @return a double value, not null
     * @throws NullPointerException if {@code number} is {@code null}
     * @throws NumberFormatException if {@code number} is not a valid representation of a {@code BigDecimal}.
     * @see #toBigDecimalThrow(int, RoundingMode)
     * @see BigDecimal#doubleValue()
     */
    public double doubleValueThrow(final int scale, final RoundingMode roundingMode){
        return toBigDecimalThrow(scale, roundingMode).doubleValue();
    }

    //-----------------------------------------------------------------------------------------------------------------
    /**
     ** 转换为Integer，如果转换失败则默认为0
     *
     * @return {@code Integer}, not null
     * @see #toIntegerDefault(Object)
     */
    public Integer toInteger(){
        return toIntegerDefault(0);
    }

    //-----------------------------------------------------------------------------------------------------------------
    /**
     ** 转换为Integer，如果转换失败则默认为0
     *
     * @param roundingMode 舍入模式，为空时默认是四舍五入模式RoundingMode.HALF_UP
     * @return {@code Integer}, not null
     * @see #toIntegerDefault(RoundingMode, Object)
     */
    public Integer toInteger(final RoundingMode roundingMode){
        return toIntegerDefault(roundingMode, 0);
    }

    //-----------------------------------------------------------------------------------------------------------------
    /**
     ** 转换为Integer
     *
     * @param defaultNumber 当转换出错时默认数字值
     * @return {@code Integer}, not null
     * @see #toIntegerThrow()
     * @see #of(Object)
     * @see #toInteger()
     */
    public Integer toIntegerDefault(final Object defaultNumber){
        try {
            return toIntegerThrow();
        }catch (NullPointerException | NumberFormatException ex){
            return of(defaultNumber).toInteger();
        }
    }

    //-----------------------------------------------------------------------------------------------------------------
    /**
     ** 转换为Integer
     *
     * @param roundingMode 舍入模式，为空时默认是四舍五入模式RoundingMode.HALF_UP
     * @param defaultNumber 当转换出错时默认数字值
     * @return {@code Integer}, not null
     * @see #toIntegerThrow(RoundingMode)
     * @see #of(Object)
     * @see #toInteger(RoundingMode)
     */
    public Integer toIntegerDefault(final RoundingMode roundingMode, final Object defaultNumber){
        try {
            return toIntegerThrow(roundingMode);
        }catch (NullPointerException | NumberFormatException ex){
            return of(defaultNumber).toInteger(roundingMode);
        }
    }

    //-----------------------------------------------------------------------------------------------------------------
    /**
     ** 转换为Integer，如果转换失败则抛出异常
     *
     * @return {@code Integer}, not null
     * @throws NullPointerException if {@code number} is {@code null}
     * @throws NumberFormatException if {@code number} is not a valid representation of a {@code BigDecimal}.
     * @see #toBigDecimalThrow()
     * @see BigDecimal#intValue()
     */
    public Integer toIntegerThrow(){
        return toBigDecimalThrow().intValue();
    }

    //-----------------------------------------------------------------------------------------------------------------
    /**
     ** 转换为Integer，如果转换失败则抛出异常
     *
     * @param roundingMode 舍入模式，为空时默认是四舍五入模式RoundingMode.HALF_UP
     * @return {@code Integer}, not null
     * @throws NullPointerException if {@code number} is {@code null}
     * @throws NumberFormatException if {@code number} is not a valid representation of a {@code BigDecimal}.
     * @see #toBigDecimalThrow(int, RoundingMode)
     * @see BigDecimal#intValue()
     */
    public Integer toIntegerThrow(final RoundingMode roundingMode){
        return toBigDecimalThrow(0, roundingMode).intValue();
    }

    //-----------------------------------------------------------------------------------------------------------------
    /**
     ** 转换为Long，如果转换失败则默认为0
     *
     * @return {@code Long}, not null
     * @see #toLongDefault(Object)
     */
    public Long toLong(){
        return toLongDefault(0);
    }

    //-----------------------------------------------------------------------------------------------------------------
    /**
     ** 转换为Long，如果转换失败则默认为0
     *
     * @param roundingMode 舍入模式，为空时默认是四舍五入模式RoundingMode.HALF_UP
     * @return {@code Long}, not null
     * @see #toLongDefault(RoundingMode, Object)
     */
    public Long toLong(final RoundingMode roundingMode){
        return toLongDefault(roundingMode, 0);
    }

    //-----------------------------------------------------------------------------------------------------------------
    /**
     ** 转换为Long
     *
     * @param defaultNumber 当转换出错时默认数字值
     * @return {@code Long}, not null
     * @see #toLongThrow()
     * @see #of(Object)
     * @see #toLong()
     */
    public Long toLongDefault(final Object defaultNumber){
        try {
            return toLongThrow();
        }catch (NullPointerException | NumberFormatException ex){
            return of(defaultNumber).toLong();
        }
    }

    //-----------------------------------------------------------------------------------------------------------------
    /**
     ** 转换为Long
     *
     * @param roundingMode 舍入模式，为空时默认是四舍五入模式RoundingMode.HALF_UP
     * @param defaultNumber 当转换出错时默认数字值
     * @return {@code Long}, not null
     * @see #toLongThrow(RoundingMode)
     * @see #of(Object)
     * @see #toLong(RoundingMode)
     */
    public Long toLongDefault(final RoundingMode roundingMode, final Object defaultNumber){
        try {
            return toLongThrow(roundingMode);
        }catch (NullPointerException | NumberFormatException ex){
            return of(defaultNumber).toLong(roundingMode);
        }
    }

    //-----------------------------------------------------------------------------------------------------------------
    /**
     ** 转换为Long，如果转换失败则抛出异常
     *
     * @return {@code Long}, not null
     * @throws NullPointerException if {@code number} is {@code null}
     * @throws NumberFormatException if {@code number} is not a valid representation of a {@code BigDecimal}.
     * @see #toBigDecimalThrow()
     * @see BigDecimal#longValue()
     */
    public Long toLongThrow(){
        return toBigDecimalThrow().longValue();
    }

    //-----------------------------------------------------------------------------------------------------------------
    /**
     ** 转换为Long，如果转换失败则抛出异常
     *
     * @param roundingMode 舍入模式，为空时默认是四舍五入模式RoundingMode.HALF_UP
     * @return {@code Long}, not null
     * @throws NullPointerException if {@code number} is {@code null}
     * @throws NumberFormatException if {@code number} is not a valid representation of a {@code BigDecimal}.
     * @see #toBigDecimalThrow(int, RoundingMode)
     * @see BigDecimal#longValue()
     */
    public Long toLongThrow(final RoundingMode roundingMode){
        return toBigDecimalThrow(0, roundingMode).longValue();
    }

    //-----------------------------------------------------------------------------------------------------------------
    /**
     ** 转换为Float，如果转换失败则默认为0
     *
     * @return {@code Float}, not null
     * @see #toFloatDefault(Object)
     */
    public Float toFloat(){
        return toFloatDefault(0);
    }

    //-----------------------------------------------------------------------------------------------------------------
    /**
     ** 转换为Float，舍入模式默认是四舍五入RoundingMode.HALF_UP，如果转换失败则默认为0
     *
     * @param scale 保存小数点后面的精度，即小数位数
     * @return {@code Float}, not null
     * @see #toFloatDefault(int, RoundingMode, Object)
     */
    public Float toFloat(final int scale){
        return toFloatDefault(scale, RoundingMode.HALF_UP, 0);
    }

    //-----------------------------------------------------------------------------------------------------------------
    /**
     ** 转换为Float，如果转换失败则默认为0
     *
     * @param scale 保存小数点后面的精度，即小数位数
     * @param roundingMode 舍入模式，为空时默认是四舍五入模式RoundingMode.HALF_UP
     * @return {@code Float}, not null
     * @see #toFloatDefault(int, RoundingMode, Object)
     */
    public Float toFloat(final int scale, final RoundingMode roundingMode){
        return toFloatDefault(scale, roundingMode, 0);
    }

    //-----------------------------------------------------------------------------------------------------------------
    /**
     ** 转换为Float，如果转换失败则默认为0
     *
     * @param defaultNumber 当转换出错时默认数字值
     * @return {@code Float}, not null
     * @see #toFloatThrow()
     * @see #of(Object)
     * @see #toFloat()
     */
    public Float toFloatDefault(final Object defaultNumber){
        try {
            return toFloatThrow();
        }catch (Exception ex){
            return of(defaultNumber).toFloat();
        }
    }

    //-----------------------------------------------------------------------------------------------------------------
    /**
     ** 转换为Float，舍入模式默认是四舍五入RoundingMode.HALF_UP，如果转换失败则默认为0
     *
     * @param scale 保存小数点后面的精度，即小数位数
     * @param defaultNumber 当转换出错时默认数字值
     * @return {@code Float}, not null
     * @see #toFloatDefault(int, RoundingMode, Object)
     */
    public Float toFloatDefault(final int scale, final Object defaultNumber){
        return toFloatDefault(scale, RoundingMode.HALF_UP, defaultNumber);
    }

    //-----------------------------------------------------------------------------------------------------------------
    /**
     ** 转换为Float，如果转换失败则默认为0
     *
     * @param scale 保存小数点后面的精度，即小数位数
     * @param roundingMode 舍入模式，为空时默认是四舍五入模式RoundingMode.HALF_UP
     * @param defaultNumber 当转换出错时默认数字值
     * @return {@code Float}, not null
     * @see #toFloatThrow(int, RoundingMode)
     * @see #of(Object)
     * @see #toFloat(int, RoundingMode)
     */
    public Float toFloatDefault(final int scale, final RoundingMode roundingMode, final Object defaultNumber){
        try {
            return toFloatThrow(scale, roundingMode);
        }catch (NullPointerException | NumberFormatException ex){
            return of(defaultNumber).toFloat(scale, roundingMode);
        }
    }

    //-----------------------------------------------------------------------------------------------------------------
    /**
     ** 转换为Float，如果转换失败则抛出异常
     *
     * @return {@code Float}, not null
     * @throws NullPointerException if {@code number} is {@code null}
     * @throws NumberFormatException if {@code number} is not a valid representation of a {@code BigDecimal}.
     * @see #toBigDecimalThrow()
     * @see BigDecimal#floatValue()
     */
    public Float toFloatThrow(){
        return toBigDecimalThrow().floatValue();
    }

    //-----------------------------------------------------------------------------------------------------------------
    /**
     ** 转换为Float，舍入模式默认是四舍五入RoundingMode.HALF_UP，如果转换失败则抛出异常
     *
     * @param scale 保存小数点后面的精度，即小数位数
     * @return {@code Float}, not null
     * @throws NullPointerException if {@code number} is {@code null}
     * @throws NumberFormatException if {@code number} is not a valid representation of a {@code BigDecimal}.
     * @see #toFloatThrow(int, RoundingMode)
     */
    public Float toFloatThrow(final int scale){
        return toFloatThrow(scale, RoundingMode.HALF_UP);
    }

    //-----------------------------------------------------------------------------------------------------------------
    /**
     ** 转换为Float，如果转换失败则抛出异常
     *
     * @param scale 保存小数点后面的精度，即小数位数
     * @param roundingMode 舍入模式，为空时默认是四舍五入模式RoundingMode.HALF_UP
     * @return a Float, not null
     * @throws NullPointerException if {@code number} is {@code null}
     * @throws NumberFormatException if {@code number} is not a valid representation of a {@code BigDecimal}.
     * @see #toBigDecimalThrow(int, RoundingMode)
     * @see BigDecimal#floatValue()
     */
    public Float toFloatThrow(final int scale, final RoundingMode roundingMode){
        return toBigDecimalThrow(scale, roundingMode).floatValue();
    }

    //-----------------------------------------------------------------------------------------------------------------
    /**
     ** 转换为Double，如果转换失败则默认为0
     *
     * @return {@code Double}, not null
     * @see #toDoubleDefault(Object)
     */
    public Double toDouble(){
        return toDoubleDefault(0);
    }

    //-----------------------------------------------------------------------------------------------------------------
    /**
     ** 转换为Double，舍入模式默认是四舍五入RoundingMode.HALF_UP，如果转换失败则默认为0
     *
     * @param scale 保存小数点后面的精度，即小数位数
     * @return {@code Double}, not null
     * @see #toDoubleDefault(int, Object)
     */
    public Double toDouble(final int scale){
        return toDoubleDefault(scale, 0);
    }

    //-----------------------------------------------------------------------------------------------------------------
    /**
     ** 转换为Double，如果转换失败则默认为0
     *
     * @param scale 保存小数点后面的精度，即小数位数
     * @param roundingMode 舍入模式，为空时默认是四舍五入模式RoundingMode.HALF_UP
     * @return {@code Double}, not null
     * @see #toDoubleDefault(int, RoundingMode, Object)
     */
    public Double toDouble(final int scale, final RoundingMode roundingMode){
        return toDoubleDefault(scale, roundingMode, 0);
    }

    //-----------------------------------------------------------------------------------------------------------------
    /**
     ** 转换为Double
     *
     * @param defaultNumber 当转换出错时默认数字值
     * @return {@code Double}, not null
     * @see #toDoubleThrow()
     * @see #of(Object)
     * @see #toDouble()
     */
    public Double toDoubleDefault(final Object defaultNumber){
        try {
            return toDoubleThrow();
        }catch (NullPointerException | NumberFormatException ex){
            return of(defaultNumber).toDouble();
        }
    }

    //-----------------------------------------------------------------------------------------------------------------
    /**
     ** 转换为Double，舍入模式默认是四舍五入RoundingMode.HALF_UP
     *
     * @param scale 保存小数点后面的精度，即小数位数
     * @param defaultNumber 当转换出错时默认数字值
     * @return {@code Double}, not null
     * @see #toDoubleDefault(int, RoundingMode, Object)
     */
    public Double toDoubleDefault(final int scale, final Object defaultNumber){
        return toDoubleDefault(scale, RoundingMode.HALF_UP, defaultNumber);
    }

    //-----------------------------------------------------------------------------------------------------------------
    /**
     ** 转换为Double
     *
     * @param scale 保存小数点后面的精度，即小数位数
     * @param roundingMode 舍入模式，为空时默认是四舍五入模式RoundingMode.HALF_UP
     * @param defaultNumber 当转换出错时默认数字值
     * @return {@code Double}, not null
     * @see #toDouble(int, RoundingMode)
     * @see #of(Object)
     * @see #toDouble(int, RoundingMode)
     */
    public Double toDoubleDefault(final int scale, final RoundingMode roundingMode, final Object defaultNumber){
        try {
            return toDoubleThrow(scale, roundingMode);
        }catch (NullPointerException | NumberFormatException ex){
            return of(defaultNumber).toDouble(scale, roundingMode);
        }
    }

    //-----------------------------------------------------------------------------------------------------------------
    /**
     ** 转换为Double，如果转换失败则抛出异常
     *
     * @return {@code Double}, not null
     * @throws NullPointerException if {@code number} is {@code null}
     * @throws NumberFormatException if {@code number} is not a valid representation of a {@code BigDecimal}.
     * @see #toBigDecimalThrow()
     * @see BigDecimal#doubleValue()
     */
    public Double toDoubleThrow(){
        return toBigDecimalThrow().doubleValue();
    }

    //-----------------------------------------------------------------------------------------------------------------
    /**
     ** 转换为Double，舍入模式默认是四舍五入RoundingMode.HALF_UP，如果转换失败则抛出异常
     *
     * @param scale 保存小数点后面的精度，即小数位数
     * @return {@code Double}, not null
     * @throws NullPointerException if {@code number} is {@code null}
     * @throws NumberFormatException if {@code number} is not a valid representation of a {@code BigDecimal}.
     * @see #toDoubleThrow(int, RoundingMode)
     */
    public Double toDoubleThrow(final int scale){
        return toDoubleThrow(scale, RoundingMode.HALF_UP);
    }

    //-----------------------------------------------------------------------------------------------------------------
    /**
     ** 转换为Double，如果转换失败则抛出异常
     *
     * @param scale 保存小数点后面的精度，即小数位数
     * @param roundingMode 舍入模式，为空时默认是四舍五入模式RoundingMode.HALF_UP
     * @return {@code Double}, not null
     * @throws NullPointerException if {@code number} is {@code null}
     * @throws NumberFormatException if {@code number} is not a valid representation of a {@code BigDecimal}.
     * @see #toBigDecimalThrow(int, RoundingMode)
     * @see BigDecimal#doubleValue()
     */
    public Double toDoubleThrow(final int scale, final RoundingMode roundingMode){
        return toBigDecimalThrow(scale, roundingMode).doubleValue();
    }

    //-----------------------------------------------------------------------------------------------------------------
    /**
     ** 转换为BigDecimal类，如果转换失败则默认为0
     *
     * @return {@code BigDecimal}, not null
     * @see #toBigDecimalDefault(Object)
     */
    public BigDecimal toBigDecimal(){
        return toBigDecimalDefault(0);
    }

    //-----------------------------------------------------------------------------------------------------------------
    /**
     ** 转换为BigDecimal类，舍入模式默认是四舍五入RoundingMode.HALF_UP，如果转换失败则默认为0
     *
     * @param scale 保存小数点后面的精度，即小数位数
     * @return {@code BigDecimal}, not null
     * @see #toBigDecimalDefault(int, Object)
     */
    public BigDecimal toBigDecimal(final int scale){
        return toBigDecimalDefault(scale, 0);
    }

    //-----------------------------------------------------------------------------------------------------------------
    /**
     ** 转换为BigDecimal类，如果转换失败则默认为0
     *
     * @param scale 保存小数点后面的精度，即小数位数
     * @param roundingMode 舍入模式，为空时默认是四舍五入模式RoundingMode.HALF_UP
     * @return {@code BigDecimal}, not null
     * @see #toBigDecimalDefault(int, RoundingMode, Object)
     */
    public BigDecimal toBigDecimal(final int scale, final RoundingMode roundingMode){
        return toBigDecimalDefault(scale, roundingMode, 0);
    }

    //-----------------------------------------------------------------------------------------------------------------
    /**
     ** 转换为BigDecimal类，如果转换失败则默认为0
     *
     * @param defaultNumber 当转换出错时默认数字值
     * @return {@code BigDecimal}, not null
     * @see #toBigDecimalThrow()
     * @see #of(Object)
     * @see #toBigDecimal()
     */
    public BigDecimal toBigDecimalDefault(final Object defaultNumber){
        try{
            return toBigDecimalThrow();
        }catch (NullPointerException | NumberFormatException ex){
            return of(defaultNumber).toBigDecimal();
        }
    }

    //-----------------------------------------------------------------------------------------------------------------
    /**
     ** 转换为BigDecimal类，舍入模式默认是四舍五入RoundingMode.HALF_UP
     *
     * @param scale 保存小数点后面的精度，即小数位数
     * @param defaultNumber 当转换出错时默认数字值
     * @return {@code BigDecimal}, not null
     * @see #toBigDecimalDefault(int, RoundingMode, Object)
     */
    public BigDecimal toBigDecimalDefault(final int scale, final Object defaultNumber){
        return toBigDecimalDefault(scale, RoundingMode.HALF_UP, defaultNumber);
    }

    //-----------------------------------------------------------------------------------------------------------------
    /**
     ** 转换为BigDecimal类
     *
     * @param scale 保存小数点后面的精度，即小数位数
     * @param roundingMode 舍入模式，为空时默认是四舍五入模式RoundingMode.HALF_UP
     * @param defaultNumber 当转换出错时默认数字值
     * @return {@code BigDecimal}, not null
     * @see #toBigDecimalThrow(int, RoundingMode)
     * @see #of(Object)
     * @see #toBigDecimal(int, RoundingMode)
     */
    public BigDecimal toBigDecimalDefault(final int scale,
                                          final RoundingMode roundingMode,
                                          final Object defaultNumber){
        try{
            return toBigDecimalThrow(scale, roundingMode);
        }catch (NullPointerException | NumberFormatException ex){
            return of(defaultNumber).toBigDecimal(scale, roundingMode);
        }
    }

    //-----------------------------------------------------------------------------------------------------------------
    /**
     ** 转换为BigDecimal，如果转换失败则抛出异常
     *
     * @return {@code BigDecimal}, not null
     * @throws NullPointerException if {@code number} is {@code null}
     * @throws NumberFormatException if {@code number} is not a valid
     *         representation of a {@code BigDecimal}.
     */
    public BigDecimal toBigDecimalThrow(){
        Objects.requireNonNull(object, "The object value cannot be null");
        if(bigDecimal != null){
            return bigDecimal;
        }
        if(object instanceof Byte){
            bigDecimal = new BigDecimal((Byte) object);
        }else if(object instanceof Short){
            bigDecimal = new BigDecimal((Short) object);
        }else if(object instanceof Integer){
            bigDecimal = new BigDecimal((Integer) object);
        }else if(object instanceof BigInteger){
            bigDecimal = new BigDecimal((BigInteger) object);
        }else if(object instanceof Long){
            bigDecimal = new BigDecimal((Long) object);
        }else if(object instanceof Float){
            bigDecimal = new BigDecimal((Float) object);
        }else if(object instanceof Double){
            bigDecimal = new BigDecimal((Double) object);
        }else if(object instanceof BigDecimal){
            bigDecimal = (BigDecimal) object;
        }else if(object instanceof char[]){
            bigDecimal = new BigDecimal((char[]) object);
        }else if(object instanceof CharSequence){
            bigDecimal = new BigDecimal(((CharSequence) object).toString());
        }else if(object instanceof Boolean){
            bigDecimal = Boolean.parseBoolean(object.toString()) ? BigDecimal.ONE : BigDecimal.ZERO;
        }else {
            bigDecimal = new BigDecimal(object.toString());
        }
        return bigDecimal;
    }

    //-----------------------------------------------------------------------------------------------------------------
    /**
     ** 转换为BigDecimal类，舍入模式默认是四舍五入RoundingMode.HALF_UP，如果转换失败则抛出异常
     *
     * @param scale 保存小数点后面的精度，即小数位数
     * @return {@code BigDecimal}, not null
     * @throws NullPointerException if {@code number} is {@code null}
     * @throws NumberFormatException if {@code number} is not a valid
     *         representation of a {@code BigDecimal}.
     * @see #toBigDecimalThrow(int, RoundingMode)
     */
    public BigDecimal toBigDecimalThrow(final int scale){
        return toBigDecimalThrow(scale, RoundingMode.HALF_UP);
    }

    //-----------------------------------------------------------------------------------------------------------------
    /**
     ** 转换为BigDecimal类，如果转换失败则抛出异常
     *
     * @param scale 保存小数点后面的精度，即小数位数
     * @param roundingMode 舍入模式，为空时默认是四舍五入模式RoundingMode.HALF_UP
     * @return {@code BigDecimal}, not null
     * @throws NullPointerException if {@code number} is {@code null}
     * @throws NumberFormatException if {@code number} is not a valid
     *         representation of a {@code BigDecimal}.
     * @see #toBigDecimalThrow()
     * @see BigDecimal#setScale(int, RoundingMode)
     */
    public BigDecimal toBigDecimalThrow(final int scale, final RoundingMode roundingMode){
        return toBigDecimalThrow().setScale(scale,
                Optional.ofNullable(roundingMode).orElse(RoundingMode.HALF_UP));
    }

    //-----------------------------------------------------------------------------------------------------------------
    /**
     ** 将数字转换为String类，如果转换失败则默认为0
     *
     * @return {@code String}, not null
     * @see #toBigDecimal()
     * @see BigDecimal#toPlainString()
     */
    public String toNString(){
        return toBigDecimal().toPlainString();
    }

    //-----------------------------------------------------------------------------------------------------------------
    /**
     ** 将数字转换为String类，舍入模式默认是四舍五入RoundingMode.HALF_UP，如果转换失败则默认为0
     *
     * @param scale 保存小数点后面的精度，即小数位数
     * @return {@code String}, not null
     * @see #toBigDecimal(int)
     * @see BigDecimal#toPlainString()
     */
    public String toNString(final int scale){
        return toBigDecimal(scale).toPlainString();
    }

    //-----------------------------------------------------------------------------------------------------------------
    /**
     ** 将数字转换为String类，如果转换失败则默认为0
     *
     * @param scale 保存小数点后面的精度，即小数位数
     * @param roundingMode 舍入模式，为空时默认是四舍五入模式RoundingMode.HALF_UP
     * @return {@code String}, not null
     * @see #toBigDecimal(int, RoundingMode)
     * @see BigDecimal#toPlainString()
     */
    public String toNString(final int scale, final RoundingMode roundingMode){
        return toBigDecimal(scale, roundingMode).toPlainString();
    }

    //-----------------------------------------------------------------------------------------------------------------
    /**
     ** 将数字转换为String类
     *
     * @param defaultNumber 当转换出错时默认数字值
     * @return {@code String}, not null
     * @see #toBigDecimalDefault(Object)
     * @see BigDecimal#toPlainString()
     */
    public String toNStringDefault(final Object defaultNumber){
        return toBigDecimalDefault(defaultNumber).toPlainString();
    }

    //-----------------------------------------------------------------------------------------------------------------
    /**
     ** 将数字转换为String类，舍入模式默认是四舍五入RoundingMode.HALF_UP
     *
     * @param scale 保存小数点后面的精度，即小数位数
     * @param defaultNumber 当转换出错时默认数字值
     * @return {@code String}, not null
     * @see #toBigDecimalDefault(int, Object)
     * @see BigDecimal#toPlainString()
     */
    public String toNStringDefault(final int scale, final Object defaultNumber){
        return toBigDecimalDefault(scale, defaultNumber).toPlainString();
    }

    //-----------------------------------------------------------------------------------------------------------------
    /**
     ** 将数字转换为String类
     *
     * @param scale 保存小数点后面的精度，即小数位数
     * @param roundingMode 舍入模式，为空时默认是四舍五入模式RoundingMode.HALF_UP
     * @param defaultNumber 当转换出错时默认数字值
     * @return {@code String}, not null
     * @see #toBigDecimalDefault(int, RoundingMode, Object)
     * @see BigDecimal#toPlainString()
     */
    public String toNStringDefault(final int scale,
                                   final RoundingMode roundingMode,
                                   final Object defaultNumber){
        return toBigDecimalDefault(scale, roundingMode, defaultNumber).toPlainString();
    }

    //-----------------------------------------------------------------------------------------------------------------
    /**
     ** 将数字转换为String类，如果转换失败则抛出异常
     *
     * @return {@code String}, not null
     * @throws NullPointerException if {@code number} is {@code null}
     * @throws NumberFormatException if {@code number} is not a valid representation of a {@code BigDecimal}.
     * @see #toBigDecimalThrow()
     * @see BigDecimal#toPlainString()
     */
    public String toNStringThrow(){
        return toBigDecimalThrow().toPlainString();
    }

    //-----------------------------------------------------------------------------------------------------------------
    /**
     ** 将数字转换为String类，舍入模式默认是四舍五入RoundingMode.HALF_UP，如果转换失败则抛出异常
     *
     * @param scale 保存小数点后面的精度，即小数位数
     * @return {@code String}, not null
     * @throws NullPointerException if {@code number} is {@code null}
     * @throws NumberFormatException if {@code number} is not a valid representation of a {@code BigDecimal}.
     * @see #toBigDecimalThrow(int)
     * @see BigDecimal#toPlainString()
     */
    public String toNStringThrow(final int scale){
        return toBigDecimalThrow(scale).toPlainString();
    }

    //-----------------------------------------------------------------------------------------------------------------
    /**
     ** 将数字转换为String类，如果转换失败则抛出异常
     *
     * @param scale 保存小数点后面的精度，即小数位数
     * @param roundingMode 舍入模式，为空时默认是四舍五入模式RoundingMode.HALF_UP
     * @return {@code String}, not null
     * @throws NullPointerException if {@code number} is {@code null}
     * @throws NumberFormatException if {@code number} is not a valid representation of a {@code BigDecimal}.
     * @see #toBigDecimalThrow(int, RoundingMode)
     * @see BigDecimal#toPlainString()
     */
    public String toNStringThrow(final int scale, final RoundingMode roundingMode){
        return toBigDecimalThrow(scale, roundingMode).toPlainString();
    }

    //-----------------------------------------------------------------------------------------------------------------
    /**
     ** 转换为字符串，如果object为null，则返回空字符串
     *
     * @return {@code String}, not null
     * @see #toStringEmpty()
     */
    @Override
    public String toString(){
        return toStringEmpty();
    }

    //-----------------------------------------------------------------------------------------------------------------
    /**
     ** 转换为字符串
     *
     * @param defaultValue 默认值
     * @return {@code String}, nullable
     * @see #toStringThrow()
     * @see #of(Object)
     * @see #toStringNull()
     */
    public String toStringDefault(final Object defaultValue){
        try{
            return toStringThrow();
        }catch (NullPointerException ex){
            return of(defaultValue).toStringNull();
        }
    }

    //-----------------------------------------------------------------------------------------------------------------
    /**
     ** 转换为字符串，如果object为null，则返回空字符串
     *
     * @return {@code String}, nullable
     * @see #toStringThrow()
     */
    public String toStringEmpty(){
        try{
            return toStringThrow();
        }catch (NullPointerException ex){
            return "";
        }
    }

    //-----------------------------------------------------------------------------------------------------------------
    /**
     ** 转换为字符串，如果object为null，则返回null
     *
     * @return {@code String}, nullable
     * @see #toStringThrow()
     */
    public String toStringNull(){
        try{
            return toStringThrow();
        }catch (NullPointerException ex){
            return null;
        }
    }

    //-----------------------------------------------------------------------------------------------------------------
    /**
     ** 转换为字符串，如果转换失败则抛出异常
     *
     * @return {@code String}, not null
     * @throws NullPointerException if {@code object} is {@code null}
     * @see String#valueOf(Object)
     */
    public String toStringThrow(){
        Objects.requireNonNull(object, "The object value cannot be null");
        return String.valueOf(object);
    }

    //-----------------------------------------------------------------------------------------------------------------


}
